<?php
include_once $_SERVER['DOCUMENT_ROOT']."/controller/connect.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Output.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/PictureOutput.php";
/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/21
 * Time: 15:04
 */
class PictureOutputDAO{
    const FIELD_ID = "outputId";
    const FIELD_ACTION_ID = "actionId";
    const FIELD_DESCRIPTION = "outputDesc";
    const FIELD_VALUE = "outputValue";
    const FIELD_WORKER_ID = "workerId";
    const FIELD_INDICATOR = "outputIndicator";

    public $conn;

    function __construct()
    {
        $this->conn = new db();
    }

    //插入图片输出
    public function insert($picture){
        $db = $this->conn->getDBConn();
		$db->query("SET NAMES UTF-8");
        $stmt = $db->prepare("insert into pictureoutput(actionId,outputDesc,outputValue,workerId,outputIndicator) values(?,?,?,?,?)");
        $stmt->bindParam(1,$picture[self::FIELD_ACTION_ID],PDO::PARAM_INT);
        $stmt->bindParam(2,mb_convert_encoding($picture[self::FIELD_DESCRIPTION],'GBK','UTF-8'));
        $stmt->bindParam(3,$picture[self::FIELD_VALUE]);
        $stmt->bindParam(4,$picture[self::FIELD_WORKER_ID],PDO::PARAM_INT);
        $stmt->bindParam(5,$picture[self::FIELD_INDICATOR],PDO::PARAM_INT);
        $result = $stmt->execute();
var_dump($stmt->errorInfo());
        return $result;
    }

    //删除图片输出
    public function delete($pictureId){

    }

    //查询图片输出
    public function query($pictureId){
        $sql = "select * from pictureoutput where ".self::FIELD_ID." = ".$pictureId;
        $db = $this->conn->getDBConn();
        $row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if($row){
            $picture = $this->loadDataFromResult($row);
            return $picture;
        }
        else
            return null;
    }

    //更新图片输出
    public function update($picture){

    }

    //返回一个action对应的所有图片输出
    public function queryOutputListByAction($actionId){
        $sql = "select * from pictureoutput where ".self::FIELD_ACTION_ID." = ".$actionId;
        $db = $this->conn->getDBConn();
        $rows = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if(count($rows) > 0){
            $pictures = array();
            foreach ($rows as $row){
                $picture = $this->loadDataFromResult($row);
                array_push($pictures,$picture);
            }
            return $pictures;
        }
        else
            return null;
    }

    //根据indicator返回一个action对应的所有图片输出
    public function queryOutputListByActionAndIndicator($actionId,$indicator){
        $sql = "select * from pictureoutput where ".self::FIELD_ACTION_ID." = ".$actionId." and ".self::FIELD_INDICATOR." = ".$indicator;
        $db = $this->conn->getDBConn();
        $rows = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if(count($rows) > 0){
            $pictures = array();
            foreach ($rows as $row){
                $picture = $this->loadDataFromResult($row);
                array_push($pictures,$picture);
            }
            return $pictures;
        }
        else
            return null;
    }

    //返回用户针对某个action的所有输出
    public function queryOutputListByActionAndUser($actionId,$userId,$indicator){
        $sql = "select * from pictureoutput where ".self::FIELD_ACTION_ID." = ".$actionId." and ".self::FIELD_INDICATOR." = ".$indicator." and ".self::FIELD_WORKER_ID." = ".$userId;
        $db = $this->conn->getDBConn();
        $rows = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if(count($rows) > 0){
            $pictures = array();
            foreach ($rows as $row){
                $picture = $this->loadDataFromResult($row);
                array_push($pictures,$picture);
            }
            return $pictures;
        }
        else
            return null;
    }

    //从结果集中构建出pictureoutput对象
    private function loadDataFromResult($row){
        $picture = new PictureOutput();
        $picture->setId($row[self::FIELD_ID]);
        $picture->setActionId($row[self::FIELD_ACTION_ID]);
        $picture->setDesc($row[self::FIELD_DESCRIPTION]);
        $picture->setValue($row[self::FIELD_VALUE]);
        $picture->setWorkerId($row[self::FIELD_WORKER_ID]);
        $picture->setIndicator($row[self::FIELD_INDICATOR]);
        return $picture;
    }
}
